exports.StoreTableViewCell = UI.ListViewItem.subclass({
    
    className: "StoreTableViewCell",
    
    initialize: function(reuseId,cellModel,pw) {
		//$super(reuseId);
    	this._parentWindow = pw;
    	this._cellView = null;
    	this._indexPath_Row = 0;
        this._cellModel = cellModel;
        this._itemId = null;
    },
    
    _onCreateView: function() {
		this._cellView = new CustomCellView();
		
		var cellBgPath = this.getBaseResourcePath() + "cell-background.png";
		var placeholderPath = this.getBaseResourcePath() + "placeholder.png";
		var buyButtonNormalPath = this.getBaseResourcePath() + "buy-normal.png";
		var buyButtonPressedPath = this.getBaseResourcePath() + "buy-pressed.png";

        this._cellView.setFrame([TCUtil.getAdjustedX(0),TCUtil.getAdjustedY(0),TCUtil.getAdjustedX(280),TCUtil.getAdjustedY(90)]);
        
		this._cellView.backgroundView = UIFactory.getView([TCUtil.getAdjustedX(10),TCUtil.getAdjustedY(5),TCUtil.getAdjustedX(275),TCUtil.getAdjustedY(90)], null, null);
		this._cellView.addChild(this._cellView.backgroundView);
		
		this._cellView.cellBg = UIFactory.getImage([TCUtil.getAdjustedX(0), TCUtil.getAdjustedY(0), TCUtil.getAdjustedX(275),TCUtil.getAdjustedY(90)], cellBgPath);
		this._cellView.backgroundView.addChild(this._cellView.cellBg);
		
//		this._cellView.placeholder = UIFactory.getImage([TCUtil.getAdjustedX(25), TCUtil.getAdjustedY(20), TCUtil.getAdjustedX(55),TCUtil.getAdjustedY(50)], placeholderPath);
//		this._cellView.backgroundView.addChild(this._cellView.placeholder);

		this._cellView.buyButton = UIFactory.getGenericButton([TCUtil.getAdjustedX(180), TCUtil.getAdjustedY(50), TCUtil.getAdjustedX(70),TCUtil.getAdjustedY(30)], buyButtonNormalPath, buyButtonPressedPath, Core.Localization.getString("BUY"), "000000", TCUtil.getAdjustedFontSize(60), null, null);
		this._cellView.addChild(this._cellView.buyButton);		
		
        this._cellView._itemName = UIFactory.getTextArea([TCUtil.getAdjustedX(100), TCUtil.getAdjustedY(10), TCUtil.adjustWidthWithDevice(190), TCUtil.adjustHeightWithDevice(30)], "", TCUtil.getAdjustedFontSize(70), UI.ViewGeometry.Gravity.Left, 'ffcc99');
		this._cellView.addChild(this._cellView._itemName);
		
		this._cellView._itemDescription = UIFactory.getTextArea([TCUtil.getAdjustedX(100), TCUtil.getAdjustedY(30), TCUtil.adjustWidthWithDevice(170), TCUtil.adjustHeightWithDevice(30)], "", TCUtil.getAdjustedFontSize(60), UI.ViewGeometry.Gravity.Left, 'ffcc99');
		this._cellView.addChild(this._cellView._itemDescription);
		
		this._cellView._itemPrice = UIFactory.getTextArea([TCUtil.getAdjustedX(100), TCUtil.getAdjustedY(50), TCUtil.adjustWidthWithDevice(170), TCUtil.adjustHeightWithDevice(30)], "", TCUtil.getAdjustedFontSize(70), UI.ViewGeometry.Gravity.Left, 'ffcc99');
		this._cellView.addChild(this._cellView._itemPrice);		
		
//		this._cellView._BigButton = UIFactory.getGenericButton([TCUtil.getAdjustedY(10),TCUtil.getAdjustedX(5),TCUtil.getAdjustedY(275),TCUtil.getAdjustedX(90)]);
//		this._cellView._BigButton.setAlpha(0.02);
//		this._cellView.addChild(this._cellView._BigButton);

        return this._cellView;
    },
    _onSetView: function(cellView) 
    {	    	
    	this._itemId = GACommonUtils.getKeyForStoreObject(this._cellModel);
    	//console.log("zzz item TYPE is"+TCUtil.getItemType(this._cellModel));
    	
    	cellView._itemName.setText(this._cellModel.name);
    	cellView._itemDescription.setText(this._cellModel.description);
    	
    	var priceForBucket = this._cellModel.buy_prices["default"];            
        var defaultBuyPrice = TCUtil.getPrice(priceForBucket);

		var currencyType = TCUtil.getCurrency(priceForBucket);
    	if(currencyType == kCurrencyType.kCurrencyTypeNuggets)
    	{
			cellView._itemPrice.setText(defaultBuyPrice +" "+Core.Localization.getString("NUGGETS"));
    	}
    	else
    	{
    		cellView._itemPrice.setText(defaultBuyPrice +" " + Core.Localization.getString("COINS"));
    	} 
    	
		if(cellView.icon)
		{
			cellView.removeChild(cellView.icon);
			cellView.icon = null;
		}    	

		var icon = new CustomStoreIcon();
		icon.setFrame([TCUtil.getAdjustedX(39), TCUtil.getAdjustedY(27), TCUtil.getAdjustedX(45), TCUtil.getAdjustedY(45)]);
		icon.load(this._cellModel);
		cellView.icon = icon;
		cellView.addChild(cellView.icon);


//		cellView._BigButton.onclick = function() 
//        {
//            this.onCellTap(this._cellModel);
//        }.bind(this);
//        
	    cellView.buyButton.controller = this;
		cellView.buyButton.onclick = function() 
        {
            this.onBuyTap(this._cellModel);
        }.bind(this);
    },
    
    onBuyTap: function(cellModel)
    {
    	MMSoundManager.playEffect(BUTTON_TAP_SOUND);
		var priceForBucket = cellModel.buy_prices["default"];            
        var defaultBuyPrice = TCUtil.getPrice(priceForBucket);    	
    	var currencyType = TCUtil.getCurrency(priceForBucket);
    	var user = MMGameState.getCurrentUser();
    	var isValidPurchase = user.isValidPurchase(currencyType, defaultBuyPrice);
    	
    	if(isValidPurchase)
    	{ 	
    		if(TCUtil.getItemType(cellModel) == kItemType.kItemLift)
    		{    			
//    			var totalElevators = ElevatorHandler.getElevatorsUIArray().getLength();
//    			var totalFloors = MMGameLayer.getMallUI().getFloorsUIArray().getLength();
//    			
//    			var elevatorLimit = totalFloors / FLOORS_PER_ELEVATOR;
//    			
//    			if(elevatorLimit > totalElevators)
//    			{
//    				ElevatorHandler.addElevator(this._itemId);
//    			}
//    			
//    			else
//    			{
//	    			var requiredFloors = (totalElevators * FLOORS_PER_ELEVATOR) + 1;
//	    			//console.log("zzz::: REQUIRED FLOORS ARE " + requiredFloors);
//	    			
//	    			var alertBox = UIFactory.getAlertBox("Message", "You need atleast "+requiredFloors+" floor(s) to buy next elevator.", ["ok"], this); 
//	    			
//	    			//TODO alert here saying k you need required floors to buy elevator				
//    			}
//    			//console.log("zzz::: LIFT BUY BUTTON TAPPED elevators are "+ totalElevators + " floors are " + totalFloors);
    		}
    		else if(TCUtil.getItemType(cellModel) == kItemType.kItemStore)
    		{
				MMGameVariables.setPurchasedItemId(this._itemId);
				MMGameVariables.setActionType(kActionType.kActionBuy);
				MMGameLayer.getControlLayer().addActionAlertWindow(Core.Localization.getString("BUY_STORE_ALERT"));
				if(!TutorialManager.getHasCompletedTutorial())
				{
					if(TutorialManager.getCurrentTutorialStep() == 10)
					{
						TutorialManager.hideTutorialPopup();
						MMGameLayer.getControlLayer().getBottomBar().hideTutorialIndicatorForMenu();
					}
				}
    		}
				
			//remove tutorial popups here...
			else if(TCUtil.getItemType(cellModel) == kItemType.kItemDecoration)
			{
				MMGameVariables.setPurchasedItemId(this._itemId);
				MMGameVariables.setActionType(kActionType.kActionBuy);
				MMGameLayer.getControlLayer().addActionAlertWindow(Core.Localization.getString("BUY_DECORATION_ALERT"));
			}
				
    		this._parentWindow.removeFromParent();
			this._parentWindow.destroy();
    	}
    	else 
    	{
    		//TODO zaki invalid purchase show alert...
    		var alertBox = UIFactory.getAlertBox(Core.Localization.getString("MESSAGE"), GALocalizationHandler.getRelativeString(Core.Localization.getString("NOT_ENOUGH_CASH_TO_BUY"),[cellModel.name]), ["ok"], this);
    		
    	}
    },
    
	getBaseResourcePath: function()
	{
		return "./Content/images/gamelayer/controllayer/storetableview/";
	},

    destroy: function() 
	{
    	
    },
});

